angular.module('starter.controllers', ["ionic","starter.services","starter.HTTPServices"])

.controller('STController', function($scope,$rootScope,MapManager,$ionicModal,HTTPManager,Show,userInfo,$state) {
	
	
//	var types=["跑腿","代驾","家政","洗车","代练","宠物"];
	
	
	
	
	
	
	
	
	
	
	
	$scope.infoModel = {};
	
	
	MapManager.showMap();
	
	$scope.mapSize = {
		width:window.innerWidth+"px",
		height:window.innerHeight+"px"
	};
	
	
		
	//建立发布内容的模态框
		$ionicModal.fromTemplateUrl("templates/updateView.html", {
	    scope: $scope,
	    animation: 'slide-in-up'
	  }).then(function(modal) {
	    $scope.modal = modal;
	  });
		$scope.updateMessage = function(){
			$scope.openModal();
		}
		$scope.back = function(){
			$scope.closeModal();
		}
	  $scope.openModal = function() {
	    $scope.modal.show();
	  };
	  $scope.closeModal = function() {
	    $scope.modal.hide();
	  };
	  //当我们用到模型时，清除它！
	  $scope.$on('$destroy', function() {
	    $scope.modal.remove();
	  });
	  // 当隐藏的模型时执行动作
	  $scope.$on('modal.hide', function() {
	    // 执行动作
	  });
	  // 当移动模型时执行动作
	  $scope.$on('modal.removed', function() {
	    // 执行动作
	  });
	  
	  
	  $scope.toUp = function(){
	  	console.log(userInfo.getInfo("userid")&&userInfo.getInfo("validity")===true);
	  	
	  	userInfo.getInfo("userid")&&userInfo.getInfo("validity")===true?
	  	$scope.modal.show():$state.go("tab.user");
	  }
	  
	  
	  //发布用户信息
	  $scope.toUpdate = function(){
	  	
	  	
	  	
	  	
	  	
	  	$scope.infoModel.userID = 0;
	  	
	  	console.log($scope.infoModel.type);
	  	
	  	if ($scope.infoModel.time){
	      $scope.infoModel.endTime = $scope.infoModel.time.getTime();
	    }
	    $scope.infoModel.endTime =$scope.infoModel.time.getTime();
	    $scope.infoModel.startTime = new Date().getTime();
	    MapManager.getCurrentLocation().then(function (point) {
	      $scope.infoModel.lat = point.lat;
	      $scope.infoModel.log = point.lng;
	      
	      
	      HTTPManager.post(HOST+ADD_NEWS,$scope.infoModel)
	      .then(function (result) {
		        console.log(result);
		        if (result.data.code == 2000){
		          $scope.modal.hide();
		        }else {
		          Show.showAlertDelay(result.data.message,3);
		        }
	      }).catch(function (error) {
	      		console.log(error);
		        //  弹出错误提示窗口
		        Show.showAlertDelay(error.message,3);
	      });
	      
	      
	    });
	  }
	
	//查询所有发布用户的信息
	$scope.getAllNews = function(){
		$http({
			method:"GET",
			url:"http://localhost:3000/news/getAllNews"
		}).then(function success(result){
			console.log(result);
		}).catch(function(error){
			console.log(error);
		})
	}
	
	//查询所有数据
	function loadNews(){
		HTTPManager.get(HOST+SEARCH_ALL_NEWS).then(function(result){
			
			console.log(result);
			if(result.data.data.length){//如果数据库有数据
				result.data.data.forEach(function(info){
					MapManager.addMarker(info,true,function(){
						$rootScope.$apply(function(scope){
							scope.isHide = true;
						});
					});
				});
			}else{
				
			}
			
		}).catch(function(){
			
		});
	}
	
	loadNews();
	
})
.controller('FriendsController', function($scope) {
})
.controller('RankController', function($scope) {
})
.controller('UserController', function($scope,userInfo) {
	//登录成功
	var info = {userid:1,phone:"137",username:"xiao",isValidity:false};
	userInfo.saveInfo(info);
})
